<template>
  <div id="talentSalesList">
    <div class="rightBox01">
      <div class="btn_group01">
        <button>达人销量榜</button>
      </div>
      <div class="rightBox-content">
        <!-- 时间 -->
        <section class>
          <!-- 时间 -->
          <Timeoptions01 v-model="time"
                         :radio_values="['日榜']"
                         class="timeOptions" />
          <!-- 搜索 -->
          <section class="search01">
            <div class="searchBox">
              <el-input v-model="keyword"
                        placeholder="请输入关键词在排行榜中搜索"
                        @keyup.enter.native="getData"></el-input>
              <div class="imgBox"
                   @click="getData">
                <!-- <img src="@/assets/wang/talentCompare/search.png" /> -->
              </div>
            </div>
          </section>
          <div class="btn_group">
            <button @click="generatorImage">
              <img src="@/assets/meng/star_map05.png" />分享
            </button>
            <button @click="export_data">
              <img src="@/assets/meng/star_map04.png" />导出
            </button>
          </div>
        </section>
        <!-- table -->
        <ul class="fixedThead"
            ref="fixedThead"
            :class="{ isShow: isFixed }"
            :style="showLeft == true ? 'left:222px' : 'left:98px'">
          <li tc>排名</li>
          <li>达人</li>
          <li tc>行业</li>
          <li tc
              style="  cursor: pointer;"
              @click="sort_fun(false, 1)"
              v-if="order_by == 'desc' && sort != 'thirtycount'">
            近30天上榜次数
            <img src="@/assets/meng/down_up.png"
                 v-if="order_by == 'desc' && sort == 'thirtycount'" />
            <img src="@/assets/meng/no_down_up.png"
                 v-else />
          </li>
          <li tc
              v-if="order_by == 'desc' && sort == 'thirtycount'">
            近30天上榜次数
            <img src="@/assets/meng/down_up.png"
                 v-if="order_by == 'desc' && sort == 'thirtycount'" />
            <img src="@/assets/meng/no_down_up.png"
                 v-else />
          </li>
          <li tc
              @click="sort_fun(false, 2)"
              style="  cursor: pointer;"
              v-if="order_by == 'desc' && sort != 'totalvideo'">
            作品数
            <img src="@/assets/meng/down_up.png"
                 v-if="order_by == 'desc' && sort == 'totalvideo'" />
            <img src="@/assets/meng/no_down_up.png"
                 v-else />
          </li>
          <li tc
              v-if="order_by == 'desc' && sort == 'totalvideo'">
            作品数
            <img src="@/assets/meng/down_up.png"
                 v-if="order_by == 'desc' && sort == 'totalvideo'" />
            <img src="@/assets/meng/no_down_up.png"
                 v-else />
          </li>
          <li tc
              @click="sort_fun(false, 3)"
              style="  cursor: pointer;"
              v-if="order_by == 'desc' && sort != 'totalfans'">
            粉丝数
            <img src="@/assets/meng/down_up.png"
                 v-if="order_by == 'desc' && sort == 'totalfans'" />
            <img src="@/assets/meng/no_down_up.png"
                 v-else />
          </li>
          <li tc
              v-if="order_by == 'desc' && sort == 'totalfans'">
            粉丝数
            <img src="@/assets/meng/down_up.png"
                 v-if="order_by == 'desc' && sort == 'totalfans'" />
            <img src="@/assets/meng/no_down_up.png"
                 v-else />
          </li>
          <!-- 新增(带日销量) -->
          <li tc
              @click="sort_fun(false, 4)"
              style="  cursor: pointer;"
              v-if="order_by == 'desc' && sort != 'volume'">
            带货日销量
            <img src="@/assets/meng/down_up.png"
                 v-if="order_by == 'desc' && sort == 'volume'" />
            <img src="@/assets/meng/no_down_up.png"
                 v-else />
          </li>
          <li tc
              v-if="order_by == 'desc' && sort == 'volume'">
            带货日销量
            <img src="@/assets/meng/down_up.png"
                 v-if="order_by == 'desc' && sort == 'volume'" />
            <img src="@/assets/meng/no_down_up.png"
                 v-else />
          </li>
          <li tc>操作</li>
        </ul>
        <table class="table">
          <thead>
            <tr>
              <th>排名</th>
              <th>达人</th>
              <th>行业</th>
              <th style="  cursor: pointer;"
                  @click="sort_fun(false, 1)"
                  v-if="order_by == 'desc' && sort != 'thirtycount'">
                近30天上榜次数
                <img src="@/assets/meng/down_up.png"
                     v-if="order_by == 'desc' && sort == 'thirtycount'" />
                <img src="@/assets/meng/no_down_up.png"
                     v-else />
              </th>
              <th v-if="order_by == 'desc' && sort == 'thirtycount'">
                近30天上榜次数
                <img src="@/assets/meng/down_up.png"
                     v-if="order_by == 'desc' && sort == 'thirtycount'" />
                <img src="@/assets/meng/no_down_up.png"
                     v-else />
              </th>
              <th @click="sort_fun(false, 2)"
                  style="  cursor: pointer;"
                  v-if="order_by == 'desc' && sort != 'totalvideo'">
                作品数
                <img src="@/assets/meng/down_up.png"
                     v-if="order_by == 'desc' && sort == 'totalvideo'" />
                <img src="@/assets/meng/no_down_up.png"
                     v-else />
              </th>
              <th v-if="order_by == 'desc' && sort == 'totalvideo'">
                作品数
                <img src="@/assets/meng/down_up.png"
                     v-if="order_by == 'desc' && sort == 'totalvideo'" />
                <img src="@/assets/meng/no_down_up.png"
                     v-else />
              </th>
              <th @click="sort_fun(false, 3)"
                  style="  cursor: pointer;"
                  v-if="order_by == 'desc' && sort != 'totalfans'">
                粉丝数
                <img src="@/assets/meng/down_up.png"
                     v-if="order_by == 'desc' && sort == 'totalfans'" />
                <img src="@/assets/meng/no_down_up.png"
                     v-else />
              </th>
              <th v-if="order_by == 'desc' && sort == 'totalfans'">
                粉丝数
                <img src="@/assets/meng/down_up.png"
                     v-if="order_by == 'desc' && sort == 'totalfans'" />
                <img src="@/assets/meng/no_down_up.png"
                     v-else />
              </th>
              <!-- 新增(带货日销量) -->
              <th @click="sort_fun(false, 4)"
                  style="  cursor: pointer;"
                  v-if="order_by == 'desc' && sort != 'volume'">
                带货日销量
                <img src="@/assets/meng/down_up.png"
                     v-if="order_by == 'desc' && sort == 'volume'" />
                <img src="@/assets/meng/no_down_up.png"
                     v-else />
              </th>
              <th v-if="order_by == 'desc' && sort == 'volume'">
                带货日销量
                <img src="@/assets/meng/down_up.png"
                     v-if="order_by == 'desc' && sort == 'volume'" />
                <img src="@/assets/meng/no_down_up.png"
                     v-else />
              </th>
              <!-- <th>电商视频点赞数增量</th> -->
              <!-- <th>直播销量（预估）</th> -->
              <th>操作</th>
            </tr>
          </thead>
          <tbody v-if="dataList.length != 0"
                 ref="capture">
            <tr v-for="(item, index) in dataList"
                :key="'d' + index"
                :id="!index ? 'tr1' : ''">
              <td>{{ index + 1 }}</td>
              <td>
                <div class="shopBox"
                     @click="talentDetails(item.author_id)">
                  <img :src="item.logo"
                       alt />
                  <div class="rightBox">
                    <p>{{ item.nickname }}</p>

                    <p>抖音号: {{ item.unique_id }}</p>
                  </div>
                </div>
              </td>
              <td>
                <span v-if="item.category != ''">{{ item.category }}</span>
                <span v-else>--</span>
              </td>
              <td>{{ format_num(item.thirty_count) }}</td>
              <td>{{ format_num(item.video_count) }}</td>
              <td>{{ format_num(item.fans_count) }}</td>
              <!-- 新增(带货日销量) -->
              <td>{{ format_num(item.dayvolume) }}</td>
              <!-- <td>{{ format_num(item.pro_favorite_incr) }}</td> -->
              <!-- <td>{{ format_num(item.live_sales) }}</td> -->
              <td>
                <div class="optionBox">
                  <button @click="talentDetails(item.author_id)">
                    查看详情
                  </button>
                </div>
              </td>
            </tr>
          </tbody>
        </table>
        <div v-loading="loading"
             v-if="loading"
             class="loading"
             element-loading-text="拼命加载中..."
             element-loading-spinner="el-icon-loading"></div>
        <p class="nothing"
           v-show="dataList.length != 0">我是有底线的～</p>
        <div class="empty addTop"
             v-show="dataList.length == 0 && !loading">
          <img src="@/assets/liu/zw.png" />
          <p>暂无数据</p>
        </div>
        <!-- 数据更新时间 -->
        <div class="updateTime"
             v-if="dataList.length != 0">
          数据更新时间：<span style="color:#888">{{ uptime }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import html2canvas from 'html2canvas'
import Timeoptions01 from '@/component/dayRankTime.vue'
export default {
  name: 'talentSalesList',
  components: { Timeoptions01 },
  data() {
    return {
      activeName: 'first',
      dataList: [],
      keyword: '',
      time: '日榜',
      order_by: 'desc', //   表格头部排序
      sort: 'thirtycount', //   表格头部排序
      loading: true,
      sortClass: 0,
      cancel: null,
      showLeft: true,
      isFixed: false,
      offsetTop: 0,
      uptime: '',
    }
  },
  computed: {},
  watch: {
    time: function (newVal, oldVal) {
      this.getData()
    },
    keyword: function (newVal, oldVal) {
      if (newVal == '') {
        this.getData()
      }
    },
    '$store.state.left_nav_flag'(old, newOld) {
      this.showLeft = newOld
      if ((newOld = false)) {
        this.setTBBar()
      } else {
        this.setTBBar()
      }
    },
  },
  created() {
    // Timeoptions01组件已经触发 这就不需要了
    // this.getData();
    // 判断是否到15:30 15:30前显示到前天 之后显示昨天
    let d = new Date()
    let h = d.getHours()
    let m = d.getMinutes()
  },
  mounted() {
    window.addEventListener('scroll', this.initHeight)
    window.addEventListener('resize', () => {
      //给浏览器窗口绑定变化大小事件
      this.setTBBar()
    })
  },
  methods: {
    // 获取子节点函数（后期可集成到一个库里）
    getChilds(pNode) {
      if (!pNode) return false
      let nodes = pNode.childNodes
      let newList = []
      for (let i = 0; i < nodes.length; i++) {
        let node = nodes[i]
        if (node.nodeType === 1) {
          //判断类型
          newList.push(node)
        }
      }
      return newList
    },
    // 设置表格表头滑动置顶
    setTBBar() {
      this.$nextTick(() => {
        let tableEl = this.$refs.tableEl
        let ulEl = this.$refs.fixedThead
        let tr1 = document.querySelector('#tr1')
        // thTrEl.style.width = tableEl.offsetWidth+'px';
        if (tr1) {
          let tds = this.getChilds(tr1)
          let liEls = this.getChilds(ulEl)
          for (let i = 0; i < liEls.length; i++) {
            if (liEls[i] && tds[i]) {
              liEls[i].style.width = tds[i].offsetWidth + 'px'
            }
          }
        }
      })
    },
    getData() {
      let that = this
      that.loading = true
      let time = that.time.split('?')[1]
      that.$axios
        .get(
          '/api/product/GetProShareRank',
          {
            params: {
              keyword: this.trimStr(that.keyword),
              time: time,
              orderby: that.sort,
              sort: that.order_by,
            },
          },
          {
            cancelToken: new that.$axios.CancelToken(function executor(c) {
              if (that.cancel != null) {
                that.cancel()
              }
              that.cancel = c
            }),
          }
        )
        .then((res) => {
          that.loading = false
          if (res.data.code == 0) {
            that.dataList = res.data.data
            that.uptime = that.dataList[0].create_time
            that.uptime = this.uptime.slice(0, -3)
            that.setTBBar()
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    // 分享
    generatorImage() {
      var that = this
      // 时间
      let date
      switch (that.time.split('?')[0]) {
        case '日榜':
          date =
            that.time.split('?')[1].split('-')[0] +
            '年' +
            that.time.split('?')[1].split('-')[1] +
            '月' +
            that.time.split('?')[1].split('-')[2] +
            '日'
          break
      }
      const loading = this.$loading({
        lock: true,
        text: '导出中...',
        spinner: 'el-icon-loading',
        customClass: 'customClass',
      })
      var shareContent
      shareContent = this.$refs.capture //需要截图的包裹的（原生的）DOM 对象
      // console.log("shareContent", shareContent);
      var width = shareContent.offsetWidth //获取dom 宽度
      var height = shareContent.offsetHeight //获取dom 高度
      var canvas = document.createElement('canvas') //创建一个canvas节点
      //   var scale = window.devicePixelRatio //定义任意放大倍数 支持小数 //   var scale = 2 //定义任意放大倍数 支持小数
      var scale = 1
      canvas.width = width * scale * scale //定义canvas 宽度 * 缩放
      canvas.height = height * scale * scale //定义canvas高度 *缩放
      canvas.style.width = width + 'px'
      canvas.style.height = height + 'px'
      canvas.getContext('2d').scale(scale, scale) //获取context,设置scale
      var opts = {
        dpi: window.devicePixelRatio,
        scale: scale, // 添加的scale 参数
        useCORS: true,
        canvas: canvas, //自定义 canvas
        logging: true, //日志开关
      }
      html2canvas(shareContent, opts).then((canvas) => {
        let str
        str = `《达人销量榜-${date}》.png`
        canvas.toBlob(
          function (blob) {
            const eleLink = document.createElement('a')
            eleLink.download = str
            eleLink.style.display = 'none'
            // 字符内容转变成blob地址
            eleLink.href = URL.createObjectURL(blob)
            // 触发点击
            document.body.appendChild(eleLink)
            eleLink.click()
            // 然后移除
            document.body.removeChild(eleLink)
          },
          'image/png',
          1
        )
        loading.close()
      })
    },
    // 导出
    // 文字加replace 数字加\t
    export_data() {
      let that = this
      // 时间
      let date
      switch (that.time.split('?')[0]) {
        case '日榜':
          date =
            that.time.split('?')[1].split('-')[0] +
            '年' +
            that.time.split('?')[1].split('-')[1] +
            '月' +
            that.time.split('?')[1].split('-')[2] +
            '日'
          break
      }
      let str = ''
      str = `排名,达人昵称,达人抖音号,行业,近30天上榜次数,作品数,粉丝数,直播销量（预估)\n`
      for (let i = 0; i < that.dataList.length; i++) {
        str += `${that.dataList[i].rank},\"${that.dataList[i].nickname.replace(
          /"/g,
          '""'
        )}\",${that.dataList[i].unique_id},\"${that.dataList[
          i
        ].category.replace(/"/g, '""')}\",${that.format_num(
          that.dataList[i].thirty_count
        )},${that.format_num(that.dataList[i].video_count)},${that.format_num(
          that.dataList[i].fans_count
        )},${that.format_num(that.dataList[i].live_sales)}\n`
      }
      var blob = new Blob([str], { type: 'text/plain;charset=utf-8' }) //解决中文乱码问题
      blob = new Blob([String.fromCharCode(0xfeff), blob], { type: blob.type })
      let object_url = window.URL.createObjectURL(blob)
      var link = document.createElement('a')
      link.href = object_url
      let str_name = ''
      str_name = `《达人销量榜-${date}》.csv`
      link.download = str_name
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
    },
    //   表格头部排序
    sort_fun(parm01, parm02) {
      if (parm01) {
        this.order_by = 'asc'
      } else {
        this.order_by = 'desc'
      }
      switch (parm02) {
        case 1:
          this.sort = 'thirtycount'
          break
        case 2:
          this.sort = 'totalvideo'
          break
        case 3:
          this.sort = 'totalfans'
          break
        case 4:
          this.sort = 'volume'
          break
      }
      this.getData()
    },
    // 达人详情
    talentDetails(id) {
      let router = this.$router.resolve({
        path: `/talent_main/tdetails/${id}/0`,
      })
      window.open(router.href, '_blank')
    },
    initHeight() {
      var scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop
      this.isFixed = scrollTop > 180 ? true : false
    },
  },
}
</script>
<style lang="less" scoped>
#talentSalesList {
  height: fit-content;
  margin-top: @margin-num;
  margin-left: @margin-num;
  margin-right: @margin-num;
  // padding-top: 17px;
  border-radius: 4px;
  min-height: 100vh;
  position: relative;
  /* 搜索框 */
  .imgBox {
    background: url('../../../assets/wang/talentCompare/search-nohover.png')
        no-repeat 10px 7px,
      #fff;
    background-size: 15px 15px;
  }
  .imgBox:hover {
    background: url('../../../assets/wang/talentCompare/search.png') no-repeat
        10px 7px,
      #fff;
    background-size: 15px 15px;
  }
  .addTop {
    margin-top: 100px;
  }
  .updateTime {
    font-size: 14px;
    color: #888;
    position: absolute;
    top: 20px;
    right: 20px;
    span {
      // font-family: DINAlternate-Bold;
      font-size: 15px;
    }
  }
  .timeOptions {
    display: inline-block;
  }
  .search01 {
    display: inline-block;
    margin-left: 14px;
    .searchBox {
      margin-top: 0 !important;
    }
  }
  .btn_group {
    text-align: right;
    float: right;
    button {
      width: 80px;
      height: 32px;
      border-radius: 4px;
      border: solid 1px #e6e6e6;
      font-size: 14px;
      // line-height: 30px;
      box-sizing: border-box;
      color: #222222;
      text-align: center;
    }
    > button:nth-child(1) {
      margin-right: 10px;
      img {
        width: 14px;
        vertical-align: -1px;
        margin-right: 6px;
      }
    }
    > button:nth-child(2) {
      img {
        width: 13px;
        vertical-align: -1px;
        margin-right: 6px;
      }
    }
  }
  table {
    margin-top: 14px;
    text-align: center;
    thead {
      img {
        width: 5px;
        height: 10px;
        vertical-align: 0;
        margin-right: 2px;
        cursor: pointer;
      }
      tr {
        th {
          padding: 0 4px;
        }
        th:nth-child(1) {
          width: 8%;
        }
        th:nth-child(2) {
          width: 18%;
          text-align: left;
          padding-left: 10px;
        }
        th:nth-child(3) {
          width: 14.8%;
          font-size: 14px;
          color: #222222;
        }
        th:nth-child(4),
        th:nth-child(5),
        th:nth-child(6),
        th:nth-child(7) {
          width: 12%;
        }
      }
    }
    tbody {
      tr {
        height: 78px;
        td:nth-child(3),
        td:nth-child(1) {
          font-size: 15px;
          color: #222222;
        }
        td:nth-child(4),
        td:nth-child(5),
        td:nth-child(6),
        td:nth-child(7) {
          font-size: 15px;
          color: #222222;
          font-family: DINAlternate-Bold;
        }
      }
    }
    // 排名
    .rank {
      display: flex;
      justify-content: center;
      align-items: center;
      span:nth-of-type(1) {
        font-size: 20px;
      }
      img {
        width: 8px;
        height: 12px;
        margin-left: 9px;
      }
      .pingImg {
        width: 12px;
        height: 8px;
      }
      span:nth-of-type(2) {
        color: #ea1b0b;
        padding-left: 6px;
      }
      .down {
        color: #1779ff !important;
      }
      .ping {
        color: #888 !important;
      }
    }
    // 达人
    .shopBox {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      text-align: left;
      object-fit: cover;
      cursor: pointer;
      img {
        width: 50px;
        height: 50px;
        border-radius: 50%;
        border: 1px solid #e5e5e5;
      }
      .rightBox {
        flex: 1;
        text-align: left;
        margin-left: 13px;
      }
      .rightBox p:nth-of-type(1) {
        font-size: 14px;
        color: #222222;
        font-weight: 600;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        width: 220px;
      }
      .rightBox p:nth-of-type(1):hover {
        color: #ff924b;
      }
      .rightBox p:nth-of-type(2) {
        font-size: 14px;
        color: #888;
        padding-top: 2px;
      }
    }
    // 操作
    .optionBox {
      border-radius: 0px 4px 4px 0px;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      button {
        width: 88px;
        height: 32px;
        background-color: transparent;
        border-radius: 4px;
        font-size: 14px;
        color: #fd7f2c;
        border: 1px solid #fd7f2c;
      }
      button:hover {
        color: #ff924b;
        border-color: #ff924b;
      }
    }
  }
  .rightBox01 {
    -webkit-box-sizing: border-box;
    box-sizing: border-box;

    height: 100%;
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
    border-radius: 6px;
    .btn_group01 {
      button {
        font-size: 14px;
        background-color: #fd7f2c;
        color: #ffffff !important;
        padding-left: 23px;
        padding-right: 23px;
        height: 40px;
        border-radius: 4px;
        margin-right: 7px;
        font-weight: 600;
      }
    }
  }
  .rightBox-content {
    background-color: #ffffff;
    border-radius: 6px;
    margin-top: 10px;
    padding: 20px 20px 0 20px;
    min-height: 100vh;
  }
  .fixedThead {
    display: none;
    position: fixed;
    top: 60px;
    left: 200px;

    border-radius: 4px;
    font-size: 14px;
    font-weight: 600;
    color: #555;
    background-color: #f5f9ff;
    height: 48px;
    line-height: 48px;
    overflow: hidden;
    z-index: 99;
    font-size: 0;
    li {
      box-sizing: border-box;
      display: inline-block;
      font-size: 14px;
    }

    &.isShow {
      display: block;
    }
    img {
      width: 5px;
      height: 10px;
      vertical-align: -1px;
    }
    img:nth-child(1) {
      margin-right: 2px;
    }
  }
  [tc] {
    text-align: center;
  }
  [tl] {
    text-align: left;
  }
  [tr] {
    text-align: right;
  }
}
</style>
